using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using Microsoft.SharePoint.Administration;
using System.Diagnostics;
using System.Reflection;
using Microsoft.SharePoint;
//using System.Web.Configuration;
using System.Configuration;
//using System.Web.Security;
using Microsoft.SharePoint.Utilities;

namespace CSP.LoggingEngine
{
    public class Utils
    {

        public static void LogError(TraceProvider.TraceSeverity severity, string errorMessage, string errorCategory, string applicationName)
        {
            // log error to ULS log
            TraceProvider.WriteTrace(0, severity, Guid.NewGuid(), Assembly.GetCallingAssembly().FullName, applicationName, errorCategory, errorMessage);
        }

        public static void LogError(string errorMessage, string errorCategory, string applicationName)
        {
            // log error to ULS log
            TraceProvider.WriteTrace(0, TraceProvider.TraceSeverity.CriticalEvent, Guid.NewGuid(), Assembly.GetCallingAssembly().FullName, applicationName, errorCategory, errorMessage);
        }

        public static void LogError(string errorMessageCustom, string errorCategory, string applicationName, Exception ex)
        {
            // create error message
            string errorMessage = ex.Message + " " + ex.StackTrace;

            // add any inner exceptions
            Exception innerException = ex.InnerException;
            while (innerException != null)
            {
                errorMessage += "Inner Error: " + innerException.Message + " " + innerException.StackTrace;
                innerException = innerException.InnerException;
            }

            // log error

            TraceProvider.WriteTrace(0, TraceProvider.TraceSeverity.CriticalEvent, Guid.NewGuid(), Assembly.GetCallingAssembly().FullName, applicationName, errorCategory, errorMessageCustom + " " +errorMessage);
        }
       
        public static void LogError(Exception ex, string applicationName)
        {
            // create error message
            string errorMessage = ex.Message + " " + ex.StackTrace;
           
            // add any inner exceptions
            Exception innerException = ex.InnerException;
            while (innerException != null)
            {
                errorMessage += "Inner Error: " + innerException.Message + " " + innerException.StackTrace;
                innerException = innerException.InnerException;
            }

            // log error
            LogError(errorMessage, ex.GetType().ToString(), applicationName);
        }

        public static void LogError(Exception ex, string applicationName, bool transferToErrorPage)
        {
            LogError(ex, applicationName);
            if (transferToErrorPage)
                SPUtility.TransferToErrorPage(ex.Message);
        }

    }
}
